<?php
/*
 * *********************************************************
 * ## Module controllers must extend MD_Controller class ##
 * *********************************************************
 */
class Ajax extends MD_Controller {
    
public function __construct()
{
    parent::__construct();

	/*
	 * ******************************************************************
	 * CHECK IF MODULE IS INSTALLED PROPERLY
	 * ******************************************************************
	 */
	$this->MODULE->config->load('module_config');	
	$short_name = $this->MODULE->config->item('short_name');
	$settings_table = $this->MODULE->config->item('settings_table');	 
	if(!$this->PACKAGE->security->app_modules->module_exist($short_name) || empty($short_name) || empty($settings_table))
	{
		$this->PACKAGE->errors->generate->code("001")->stop();
	}
    
            /*
             * ********************************************
             * LOAD LANGUAGE FILES FOR THIS MODULE ONLY
             * notice that some language files have been
             * loaded within MY_Controller core class
             * and those language files are common for all
             * modules
             * ********************************************
             */    
    
            $this->MODULE->lang->load('module_texts',LANGUAGE);
            
            /*
             * ********************************************
             * LOAD PACKAGES
             * ********************************************
             */
            $this->PACKAGE->core->packages->add('regional');   
			$this->PACKAGE->core->packages->add('routing');   
      
			/*
			 * ********************************************
			 * LOAD LIBRARIES
			 * ********************************************
			 */

	  
            /*
             * ********************************************
             * LOAD MODELS
             * ********************************************
             */
            $this->PACKAGE->regional->load->model('countries');
            $this->PACKAGE->regional->load->model('timezone');
			
			$this->MODULE->load->model('module_mod_carriers');
						
}

public function enable_carrier($carrier_id)
{
	/*
	 * ********************************************************************************************************************
	 * ENABLING / DISABLING CARRIER REQUIRES CHANGES IN MANY OTHER SEGMENTS OF THE APPLICATION THAT BELONG TO OTHER MODULES
	 * (FOR EXAMPLE,WHEN YOU DISABLE CARRIER THE SYSTEM MUST DISABLE ALL THE TRUNKS RELATED TO THAT CARRIER)
	 * CONSIDERING WE DON'T WANT TO LOAD ALL MODULES THAT MUST ACT IN THIS CASE, WE DO THOSE CHANGES IN CORE (APPLICATION)
	 * LIBRARIES AND MODULES. CONSIDERING THAT THE BACKEND APPLICATION MANAGES THE BACKEND ONLY, THE LOGIC STAYS CORRECT!!!
	 * ********************************************************************************************************************
	 */ 
	$this->APPLICATION->load->library('app_carriers');

	$response = $this->APPLICATION->app_carriers->enable_carrier($carrier_id);
	
	print $response;
}

public function disable_carrier($carrier_id)
{
	/*
	 * ********************************************************************************************************************
	 * ENABLING / DISABLING CARRIER REQUIRES CHANGES IN MANY OTHER SEGMENTS OF THE APPLICATION THAT BELONG TO OTHER MODULES
	 * (FOR EXAMPLE,WHEN YOU DISABLE CARRIER THE SYSTEM MUST DISABLE ALL THE TRUNKS RELATED TO THAT CARRIER)
	 * CONSIDERING WE DON'T WANT TO LOAD ALL MODULES THAT MUST ACT IN THIS CASE, WE DO THOSE CHANGES IN CORE (APPLICATION)
	 * LIBRARIES AND MODULES. CONSIDERING THAT THE BACKEND APPLICATION MANAGES THE BACKEND ONLY, THE LOGIC STAYS CORRECT!!!
	 * ********************************************************************************************************************
	 */ 
	$this->APPLICATION->load->library('app_carriers');

	$response = $this->APPLICATION->app_carriers->disable_carrier($carrier_id);
	
	print $response;
}

public function check_username()
{
	$post= $this->input->post('data');
	$username = $post['username'];

		$response = $this->MODULE->module_mod_carriers->check_username($username);

		if($response['result'] == true)
		{
			$response = "true|".$response['text'];
		}
		else
		{
			$response = "false|".$response['text'];
		}
		
   print $response;
}

    
public function save_settings()
{
	$this->MODULE->load->model('module_mod_settings');
    
    $response = $this->MODULE->module_mod_settings->save_settings($this->input->post('data'));
    
    print $response;

}

public function add_new_carrier()
{
	$response = $this->MODULE->module_mod_carriers->add_new_carrier($this->input->post('data'));	
  
    print $response;
    
}

public function edit_carrier($carrier_id)
{
	$response = $this->MODULE->module_mod_carriers->edit_carrier($this->input->post('data'),$carrier_id);	
  
    print $response;
    
}

}


?>
